﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DllInject.Properties
{
    class Native
    {
        public static long STANDARD_RIGHTS_REQUIRED = 0x000F0000L;
        public static long SYNCHRONIZE = 0x00100000L;
        public static uint PROCESS_ALL_ACCESS = (uint)(STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFFF);



        [StructLayout(LayoutKind.Sequential)]
        public struct ModuleInformation
        {
            public IntPtr lpBaseOfDll;
            public uint SizeOfImage;
            public IntPtr EntryPoint;
        }

        internal enum ModuleFilter
        {
            LIST_MODULES_DEFAULT = 0x0,
            LIST_MODULES_32BIT = 0x01,
            LIST_MODULES_64BIT = 0x02,
            LIST_MODULES_ALL = 0x03,
        }


        //指定进程的虚拟空间保留或提交内存区域
        [DllImport("kernel32.dll")]
        public static extern int VirtualAllocEx(IntPtr hwnd, Int32 lpaddress, int size, int type, Int32 tect);


        //写入某一进程的内存区域。
        [DllImport("kernel32.dll")]
        public static extern Boolean WriteProcessMemory(IntPtr hwnd, int baseaddress, string buffer, int nsize, int filewriten);


        //检索指定的动态链接库(DLL)中的输出库函数地址
        [DllImport("kernel32.dll")]
        public static extern int GetProcAddress(int hwnd, string lpname);


        //获取一个应用程序或动态链接库的模块句柄
        [DllImport("kernel32.dll")]
        public static extern int GetModuleHandleA(string name);




        //获取一个应用程序或动态链接库的模块句柄
        [DllImport("kernel32.dll")]
        public static extern int GetModuleHandleW(long name);


        //创建一个在其它进程地址空间中运行的线程(也称:创建远程线程).
        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateRemoteThread(IntPtr hwnd, int attrib, int size, int address, int par, int flags, int threadid);


        //检测hHandle事件的信号状态
        [DllImport("kernel32.dll")]
        public static extern Int32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds);


        //取消或者释放调用进程的虚地址空间页的一个区域
        [DllImport("kernel32.dll")]
        public static extern Boolean VirtualFree(IntPtr lpAddress, Int32 dwSize, Int32 dwFreeType);


        //根据窗口句柄获得窗口ID
        [DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
        private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int pid);


        //检索满足指定过滤条件的指定进程中每个模块的句柄。
        [DllImport("psapi.dll")]
        public static extern bool EnumProcessModulesEx(IntPtr hProcess, ulong[] lphModule, int cb, [MarshalAs(UnmanagedType.U4)] out uint lpcbNeeded, int flag);


        [DllImport("psapi.dll")]
        public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In][MarshalAs(UnmanagedType.U4)] uint nSize);


        //在MODULEINFO结构中检索有关指定模块的信息 。
        [DllImport("kernel32.dll")]
        public static extern bool K32GetModuleInformation(IntPtr hProcess, ulong hModule, out ModuleInformation lpmodinfo, uint cb);




        //打开现有的本地过程对象。
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);


        //检索包含指定模块的文件的标准路径。
        [DllImport("kernel32.dll")]
        public static extern int K32GetModuleFileNameExW(IntPtr hProcess, ulong hModule, byte[] lpFilename, int nSize);

        //检索包含指定模块的文件的标准路径。
        [DllImport("psapi.dll")]
        public static extern int GetModuleFileNameExW(IntPtr hProcess, ulong hModule, byte[] lpFilename, int nSize);



        public class Module
        {
            public Module(string moduleName, IntPtr baseAddress, uint size)
            {
                this.ModuleName = moduleName;
                this.BaseAddress = baseAddress;
                this.Size = size;
            }

            public string ModuleName { get; set; }
            public IntPtr BaseAddress { get; set; }
            public uint Size { get; set; }

        }

    }

}
